/*
 * Copyright (c) 2024 Huawei Technologies Co.,Ltd.
 *
 * openGauss is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *
 * http://license.coscl.org.cn/MulanPSL2
 *
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 * -------------------------------------------------------------------------
 *
 * htap_xlog_redoer.h
 * API for the HTAP XLog Redoer
 *
 * IDENTIFICATION
 * src/gausskernel/storage/htap/htap_xlog_redoer.h
 *
 * -------------------------------------------------------------------------
 */

#ifndef HTAP_XLOG_REDOER_H
#define HTAP_XLOG_REDOER_H

#include <mutex>
#include "gs_thread.h"
#include "htap_common.h"

namespace htap {
/**
 * @class HTAPXLogRedoer
 * @brief Interface for HTAP XLog Redoer
 */
class HTAPXLogRedoer {
public:
    /** @brief Creates the single XLogRedoer instance. */
    static bool CreateInstance();

    /** @brief Destroys the single XLogRedoer instance. */
    static void DestroyInstance();

    /** @brief Retrieves reference to the single XLogRedoer instance. */
    static HTAPXLogRedoer *GetInstance();

    /* *
     * @brief Launches the XLogRedoer thread.
     * @param threadId The resulting XLogRedoer thread identifier.
     * @return @ref HTAP_RC_OK if succeeded, otherwise error code.
     */
    RC Start(ThreadId &threadId);

    /* *
     * @brief Orders the XLogRedoer thread to stop.
     * This is a blocking call that waits until the XLogRedoer thread stops.
     * @return @ref HTAP_RC_OK if succeeded, otherwise error code.
     */
    RC Stop();

    /** @brief Queries whether the XLogRedoer thread is currently running. */
    bool IsRunning();

    /** @brief Retrieves the XLogRedoer thread id. */
    ThreadId GetThreadId();

    /** @brief Executes the XLogRedoer thread's main loop. */
    void RunXLogRedoer();

private:
    /** @brief Constructor. */
    HTAPXLogRedoer() : m_isRunning(false) {}

    /** @brief Destructor. */
    ~HTAPXLogRedoer() {}

    /** @var The single instance. */
    static HTAPXLogRedoer *m_xLogRedoer;

    /** @var XLogRedoer thread identifier. */
    ThreadId m_xLogRedoerThread;

    /** @var XLogRedoer thread state. */
    bool m_isRunning;

    bool m_confEnableXLogRedoer;

    /** @var Lock to serialize thread state management. */
    std::mutex m_lock;

    /** @var Memory controller */
    MemoryContext m_memory_controller;

    /** @var Cycle Memory controller - created and released each cycle */
    MemoryContext m_cycle_memory_controller;

    /** @brief Initializes the xlog redo thread. */
    void InitXLogRedoThread();

    /** @brief Executes a single Flush cycle. */
    void XLogRedo();

    /* *
     * @brief Queries whether the Flush thread was signaled to stop.
     * @param timeoutMillis The timeout in milliseconds to wait for the stop latch to be signaled.
     * @return True if the xlog redo thread was signaled to stop, otherwise false.
     * @note This is a blocking call for the specified timeout.
     */
    bool SignaledToStop(uint64_t timeoutMillis);
};
} // namespace htap
#endif // HTAP_XLOG_REDOER_H
